package com.literature.threeGroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.literature.threeGroup.mapper.*;
import com.literature.threeGroup.pojo.*;
import com.literature.threeGroup.service.CommonService;
import com.literature.threeGroup.service.LinXuanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName LinXuanServiceImpl
 * @Description: TODO
 * @Author ForeverLoveT
 * @Date 2020/1/8
 * @Version V1.0
 **/
@Service
@Transactional
public class LinXuanServiceImpl implements LinXuanService {
    @Autowired
    private ResourceInfoMapper resourceinfoMapper;
    @Autowired
    private OrganMapper organMapper;
    @Autowired
    private ParamMapper paramInfoMapper;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ResourceSnapshotMapper snapshotMapper;
    @Autowired
    private OrderMapper orderMapper;
    //查询待遴选的状态  为1
    public Page<ResourceInfo> findStatusDselect(ResourceInfo resourceInfo, int page, int size){
        PageHelper.startPage(page,size);
        Page<ResourceInfo> resourceinfos = (Page<ResourceInfo>) resourceinfoMapper.findStatusDselect(resourceInfo);
        for (ResourceInfo resInfo:resourceinfos) {
            resInfo.setLanguageName(commonService.getParamNameById(resInfo.getLanguage()));//语种名称
            resInfo.setCountryName(commonService.getParamNameById(resInfo.getCountry()));//国别名称
            resInfo.setCarrierName(commonService.getParamNameById(resInfo.getCarrier()));//载体名称
            resInfo.setCoverName(commonService.findCoverName(resInfo.getCoverids()));//套名称名称
            resInfo.setOrganName(commonService.findOrganName(resInfo.getOrganId()));//获取单位名称
        }
        return resourceinfos;
    }
    //查询待分配的状态  为2
    @Override
    public Page<ResourceInfo> findStatusDDfen(ResourceInfo resourceInfo, int page, int size) {
        PageHelper.startPage(page,size);
        Page<ResourceInfo> resourceinfos = (Page<ResourceInfo>) resourceinfoMapper.findStatusDselect(resourceInfo);
        for (ResourceInfo resInfo:resourceinfos) {
            resInfo.setLanguageName(commonService.getParamNameById(resInfo.getLanguage()));//语种名称
            resInfo.setCountryName(commonService.getParamNameById(resInfo.getCountry()));//国别名称
            resInfo.setCarrierName(commonService.getParamNameById(resInfo.getCarrier()));//载体名称
            resInfo.setCoverName(commonService.findCoverName(resInfo.getCoverids()));//套名称名称
            resInfo.setOrganName(commonService.findOrganName(resInfo.getOrganId()));//获取单位名称
        }
        return resourceinfos;
    }
    //查询待确认的状态  为3
    @Override
    public Page<ResourceInfo> findStatusDqueRen(ResourceInfo resourceInfo, int page, int size) {
        PageHelper.startPage(page,size);
        Page<ResourceInfo> resourceinfos = (Page<ResourceInfo>)resourceinfoMapper.findStatusDselect(resourceInfo);
        for (ResourceInfo resInfo:resourceinfos) {
            resInfo.setLanguageName(commonService.getParamNameById(resInfo.getLanguage()));//语种名称
            resInfo.setCountryName(commonService.getParamNameById(resInfo.getCountry()));//国别名称
            resInfo.setCarrierName(commonService.getParamNameById(resInfo.getCarrier()));//载体名称
            resInfo.setCoverName(commonService.findCoverName(resInfo.getCoverids()));//套名称名称
            resInfo.setOrganName(commonService.findOrganName(resInfo.getOrganId()));//获取单位名称
        }
        return resourceinfos;
    }
    //查询单位
    @Override
    public List<Organ> findOrgan() {
        return organMapper.selectByExample(null);
    }
    //修改状态值
    //批量单条
    @Override
    public void UpdateStatus(String[] ids, int distributionStatus) {
        if (distributionStatus==1){//待遴选改为待分配
            commonService.UpdateStatus(ids,2);
        }
        else if(distributionStatus==2){//待分配改为待确认
            commonService.UpdateStatus(ids,3);
        }else{
            return;
        }
    }
    //部门遴选修改状态
    @Override
    public void addOrganToResourcesAndUpdateStatus(String[] ids, String id) {
        for (String rid:ids) {
            ResourceInfoWithBLOBs resourceInfo = resourceinfoMapper.findOne(rid);
            //获取到当前要分配的id  的数据  把单位id存入  修改状态
            resourceInfo.setOrganId(id);
            //添加部门id到资源表
            resourceInfo.setDistributionStatus(3);
            resourceinfoMapper.updateByPrimaryKey(resourceInfo);
        }

    }
    //同意
    @Override
    public void tongYi(String[] ids) {
        //修改状态为4
        commonService.UpdateStatus(ids,4);
        //查询到ids
        List<ResourceInfoWithBLOBs> list=resourceinfoMapper.selectResources(ids);
        System.err.println(list);
        for (ResourceInfoWithBLOBs l :list) {
            ResourceSnapshotWithBLOBs withBLOBs = new ResourceSnapshotWithBLOBs();
            System.err.println(withBLOBs);
            BeanUtils.copyProperties(l, withBLOBs);
            System.err.println(withBLOBs);
            //添加到资源快照里面
            snapshotMapper.insert(withBLOBs);
            //把id塞到订单表的资源快照id生成订单
            Order order = new Order();
            order.setId(UUID.randomUUID().toString());
            order.setTitle(l.getTitle());
            order.setAgencyCompanyId("");
            order.setCarrier("");
            order.setOrderOrganization("");
            order.setCreateDate(new Date());
            order.setDatabaseSupport("");
            order.setDiscontinue(new Date());
            order.setDoorNoId("");
            order.setEstimatedAmount(0.0);
            order.setFillingMoney(0.0);
            order.setFinancialSupportId("");
            order.setForeignPrice(0.0);
            order.setIsArrival("0");
            order.setOpenRange("");
            order.setOrderState("");
            order.setOrderYear("");
            order.setPay(0.0);
            order.setPrinterMethodId("");
            order.setReturnPrice(0.0);
            order.setReviewStatus("");
            order.setSettlementPrice(0.0);
            order.setUpdateDate(new Date());

        }
    }
    //不同意写出驳回理由
    @Override

    public void boHui(String[] ids,String note) {
        for (String id:ids) {
            ResourceInfoWithBLOBs one = resourceinfoMapper.findOne(id);
            System.err.println(one);
            one.setDistributionStatus(1);//改成待遴选状态
            one.setNote(note);//驳回原因
            resourceinfoMapper.updateByPrimaryKey(one);//修改
            System.err.println(one);

        }
    }
}
